package be.idamf.cortizone.ioc.container
{
	import flash.events.Event;
	
	import mx.rpc.IResponder;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.fail;
	import org.flexunit.async.Async;

	public class BeanProxyTest
	{		
		[BeforeClass]
		public static function setUpBeforeClass():void
		{
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
		}
		
		[Before]
		public function setUp():void
		{
		}
		
		[After]
		public function tearDown():void
		{
		}
		
		[Test(async, timeout=1000)]
		public function testMethod():void
		{
			var handler:Function;
			var beanProxy:BeanProxy;
			
			handler = Async.asyncHandler(this, beanProxy_completeHandler, 500);
			
			beanProxy = new BeanProxy();
			beanProxy.type = IBeanProxyTestType;
			beanProxy.target = new BeanProxyTestTarget();
			beanProxy.addEventListener(Event.COMPLETE, handler);

			function beanProxy_completeHandler(event:Event, ... args):void
			{
				assertEquals("testMethod/Hello World!", IBeanProxyTestType(beanProxy.value).testMethod("Hello World!"));
			}
		}

		[Test(async, timeout=1000)]
		public function testService():void
		{
			var beanProxy:BeanProxy;
			var responder:IResponder;
			
			responder = Async.asyncResponder(this, new Responder(service_resultHandler, service_faultHandler), 500);
			
			beanProxy = new BeanProxy();
			beanProxy.type = IBeanProxyTestService;
			beanProxy.target = new BeanProxyTestMockService();
			beanProxy.addEventListener(Event.COMPLETE, beanProxy_completeHandler);
			
			function beanProxy_completeHandler(event:Event):void
			{
				IBeanProxyTestService(beanProxy.value).getUser("Clifton").addResponder(responder);
			}
			
			function service_resultHandler(event:ResultEvent, ... args):void
			{
				assertEquals("Clifton", event.result);
			}
			
			function service_faultHandler(event:FaultEvent, ... args):void
			{
				fail("Should not be here...");
			}
		}
	}
}
